package cn.study.aviplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.concurrent.atomic.AtomicBoolean;

public class BitmapPlayerActivity extends AbstractPlayerActivity {


    private final AtomicBoolean isPlaying = new AtomicBoolean();
    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap_player);

        surfaceView = findViewById(R.id.surface_view);

        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(shCallback);
    }


    private final SurfaceHolder.Callback shCallback = new SurfaceHolder.Callback2() {
        @Override
        public void surfaceRedrawNeeded(SurfaceHolder holder) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            isPlaying.set(true);
            new Thread(renderer).start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

            isPlaying.set(false);
        }
    };


    private final Runnable renderer = new Runnable() {

        @Override
        public void run() {

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);


            int width = getWidth(avi);
            int height = getHeight(avi);


            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

            float sx = 2.0f;
            float sy = 2.0f;

            Matrix matrix = new Matrix();
            matrix.postScale(sx, sy);

            int vWidth = (int) (width * sx);
            int vHeight = (int) (height * sy);

            int x = (1080 - vWidth) / 2;
            int y = (1920 - vHeight) / 2;

            matrix.postTranslate(x, y);
            //延迟
            long frameDelay = (long) (1000 / getFrameRate(avi));
            while (isPlaying.get()) {
                render(avi, bitmap);

                Canvas canvas = surfaceHolder.lockCanvas();
                canvas.drawBitmap(bitmap, matrix, null);
                surfaceHolder.unlockCanvasAndPost(canvas);

                //等待下一针
                try {
                    Thread.sleep(frameDelay);

                } catch (InterruptedException e) {

                    break;
                }
            }

        }
    };

    /**
     * 从avi文件描述符输出到指定bitmap来渲染帧
     *
     * @param avi
     * @param bitmap
     * @return
     */
    private native static boolean render(long avi, Bitmap bitmap);
}
